<template>
  <div class="mod-assess__assesspoint">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.naturalVillageName" placeholder="自然村名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-upload ref="upload" style="margin-left: 10px; float: right" class="filter-item" name="file" action="string" :limit="10" accept=".xlsx,.xls,.csv" :show-file-list="false" :http-request="uploadFile">
          <el-button icon="el-icon-edit" type="primary" size="small">上传Excel </el-button>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button v-if="state.hasPermission('assess:assesspoint:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column prop="name" label="点位名称" header-align="center" align="center" width="120"></el-table-column> -->
      <el-table-column prop="cityName" label="市级" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="areaName" label="区级" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="streetName" label="街道" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="adminVillageName" label="行政村" header-align="center" align="center" width="120"></el-table-column>

      <el-table-column prop="naturalVillageName" label="自然村" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="villageType" label="村庄类型" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="villageName" label="村庄全称" header-align="center" align="center" width="200"></el-table-column>
      <el-table-column prop="farmerNumber" label="农户数" header-align="center" align="center" width="100"></el-table-column>
      <el-table-column prop="status" label="状态" header-align="center" align="center" width="100">
        <template v-slot="scope">
          <span>{{ scope.row.status == 0 ? "停用" : "正常" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="lng" label="经度" header-align="center" align="center" width="100"></el-table-column>
      <el-table-column prop="lat" label="纬度" header-align="center" align="center" width="100"></el-table-column>
      <!-- <el-table-column prop="creator" label="创建者" header-align="center" align="center" width="100"></el-table-column> -->
      <el-table-column prop="createDate" label="创建时间" header-align="center" align="center" width="150">
        <template v-slot="scope">{{ scope.row.createDate && moment(scope.row.createDate).format("YYYY-MM-DD") }}</template>
      </el-table-column>
      <!-- <el-table-column prop="updater" label="更新者" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updateDate" label="更新时间" header-align="center" align="center" width="100">
        <template v-slot="scope">{{ scope.row.updateDate && moment(scope.row.updateDate).format("YYYY-MM-DD") }}</template>
      </el-table-column> -->
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="160">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('assess:assesspoint:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('assess:assesspoint:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import moment from "moment";
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./assesspoint-add-or-update.vue";
import baseService from "@/service/baseService";
import { ElLoading, ElMessage } from "element-plus";
import { fileHandle } from "@/utils/utils";

const view = reactive({
  getDataListURL: "/assess/point/page",
  getDataListIsPage: true,
  exportURL: "/assess/point/export",
  deleteURL: "/assess/point",
  deleteIsBatch: true,
  dataForm: {
    naturalVillageName: ""
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const uploadFile = async (param: any) => {
  const File = param.file;
  const formData1 = new FormData();
  formData1.append("file", File);
  formData1.append("city", "320400");
  formData1.append("cityName", "常州市");
  const loading = ElLoading.service({
    lock: true,
    text: "正在上传",
    background: "rgba(0, 0, 0, 0.7)"
  });
  try {
    console.log("jj-param", param);
    baseService
      .post("/assess/point/import", formData1)
      .then((res) => {
        console.log("res==", res);
        loading.close();
        // 刷新数据
        state.getDataList();
        ElMessage.success("上传成功");
        // filePath 返回filePath，如果有返回说明有报错，如果没返回说明没报错
      })
      .catch((err) => {
        console.log("err-", err);
        fileHandle(err, "点位导入错误表", "xlsx");
        loading.close();
      });
  } catch (e) {
    // loadingInstance.close()
    param.onError(e);
  }
};
const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};
</script>
